﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem.UControls
{
        public partial class UCPanel : Panel
        {
                public UCPanel()
                {
                        InitializeComponent();
                        base.BackColor = Color.Transparent;
                }
                private const int WM_PAINT = 0xF;
                private Color borderColor = Color.Red;
                [DefaultValue(typeof(Color), "Red"), Description("控件边框颜色")]
                public Color BorderColor
                {
                        get { return borderColor; }
                        set
                        {
                                borderColor = value;
                                Invalidate();
                        }
                }

                private int borderWidth = 0;
                public int BorderWidth
                {
                        get { return borderWidth; }
                        set
                        {
                                borderWidth = value;
                                Invalidate();
                        }
                }

                [DefaultValue(typeof(Color), "Gray"), Description("控件背景颜色")]
                private Color bgColor = Color.Gray;
                public Color BgColor
                {
                        get { return bgColor; }
                        set
                        {
                                bgColor = value;
                                Invalidate();
                        }
                }

                [DefaultValue(typeof(Color), "Gray"), Description("控件背景颜色")]
                private Color bgColor2 = Color.White;
                public Color BgColor2
                {
                        get { return bgColor2; }
                        set
                        {
                                bgColor2 = value;
                                Invalidate();
                        }
                }


                [DefaultValue(typeof(int), "5"), Description("圆角弧度大小")]
                private int radius = 5;
                public int Radius
                {
                        get { return radius; }
                        set
                        {
                                radius = value;
                                Invalidate();
                        }
                }

                [DefaultValue(typeof(LinearGradientMode), "Vertical"), Description("渐变方式")]
                private LinearGradientMode gradientMode = LinearGradientMode.Vertical;
                public LinearGradientMode GradientMode
                {
                        get { return gradientMode; }
                        set
                        {
                                gradientMode = value;
                                Invalidate();
                        }
                }
                protected override void WndProc(ref Message m)
                {
                        try
                        {
                                base.WndProc(ref m);
                                if (m.Msg == WM_PAINT)
                                {
                                        if (this.Radius > 0)
                                        {
                                                using (Graphics g = Graphics.FromHwnd(this.Handle))
                                                {
                                                        g.SmoothingMode = SmoothingMode.AntiAlias;
                                                        Rectangle r = new Rectangle();
                                                        r.Width = this.Width;
                                                        r.Height = this.Height;
                                                        DrawBorder(g, r, this.Radius);

                                                }
                                        }
                                }
                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                        }
                }

                private void DrawBorder(Graphics g, Rectangle rect, int radius)
                {
                        rect.Width -= 1;
                        rect.Height -= 1;

                        GraphicsPath path = new GraphicsPath();
                        path = PaintCommon.GetRoundRectangle(rect, radius);
                        if (BorderWidth > 0)
                        {
                                using (Pen pen = new Pen(this.BorderColor, 1))
                                {
                                        g.DrawPath(pen, path);
                                }
                        }
                        Rectangle rect1 = new Rectangle(rect.X + 1, rect.Y + 1, rect.Width - 2, rect.Height - 2);
                        DrawBackColor(g, rect1, radius);
                }

                private void DrawBackColor(Graphics g, Rectangle rect, int radius)
                {
                        GraphicsPath path1 = PaintCommon.GetRoundRectangle(rect, radius);
                        if (this.BgColor2 != Color.Transparent)
                        {
                                //线型渐变
                                LinearGradientBrush brush = new LinearGradientBrush(rect, BgColor, BgColor2, gradientMode);
                                g.FillPath(brush, path1);
                        }
                        else
                        {
                                Brush b = new SolidBrush(BgColor);
                                g.FillPath(b, path1);
                        }
                }
        }
}
